//
//  FamousTeacherViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/8.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "FamousTeacherViewController.h"
#import "FamousTeacherTableViewCell.h"
#import "TeacherDetailViewController.h"

@interface FamousTeacherViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *allArray;
@end

@implementation FamousTeacherViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)allArray{
    if (!_allArray) {
        _allArray = [NSMutableArray array];
    }
    return _allArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"飞鸽传书";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FamousTeacherTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"FamousTeacherTableViewCell"];
    [self creatDataList];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppTeacherGetTeacherList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            NSArray *array = [NSArray arrayWithArray:response];
            self.allArray = [NSMutableArray arrayWithArray:array];
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSArray *array = [NSArray arrayWithArray:obj];
                NSMutableArray *currentArray = [[NSMutableArray alloc]init];
                [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [FamousTeacherInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{@"teacherId" : @"id"};
                    }];
                    [currentArray addObject:[FamousTeacherInfo mj_objectWithKeyValues:obj]];
                }];
                [self.dataArray addObject:currentArray];
            }];
            MyLog(@"%@",self.dataArray);
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FamousTeacherTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"FamousTeacherTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.famousTeacherInfo = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FamousTeacherInfo *famousTeacherInfo = self.dataArray[indexPath.section][indexPath.row];
    
    TeacherDetailViewController *teacherDetailVC = [[TeacherDetailViewController alloc]init];
    teacherDetailVC.famousTeacherInfo = famousTeacherInfo;
    [self.navigationController pushViewController:teacherDetailVC animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 44);
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(15, 12, 200, 20);
    label.font = [UIFont systemFontOfSize:18];
    label.text = !section ? @"与时名师":section == 1 ? @"学校老师":@"大学生志愿者";
    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 10);
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
